package com.ihg.hiex.admin.service;

import org.apache.commons.lang3.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;

/**
 * 
 * @author Ysy
 *
 */
@Service("genericDataService")
public class GenericDataServiceImpl implements GenericDataService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.fms.admin.service.LoadDataService#load(java.lang.Class, java.lang.Long)
     */
    public <T> T load(Class<T> clazz, Long id) {
        
        if(id == null || id.longValue() == 0) {
            return null;
        }
        
        return genericDaoSupport.searchForObject("FROM " + ClassUtils.getShortClassName(clazz) + " WHERE disabled = false AND id = :id", "id", id, clazz);
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.fms.admin.service.GenericDataService#load(java.lang.Class, java.lang.String)
     */
    public <T> T load(Class<T> clazz, String code) {
        return genericDaoSupport.searchForObject("FROM " + ClassUtils.getShortClassName(clazz) + " WHERE disabled = false AND code = :code", "code", code, clazz);
    }
   
}
